<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initMain()
    {
        // create aliases
        class_alias('Zend_Registry', 'ZReg');
        
        // config
        $config = new Zend_Config_Ini(  __DIR__ . '/configs/application.ini', 
                                        APPLICATION_ENV,
                                        array('allowModifications' => NULL));
        ZReg::set('config', $config);
        
        // library
        Zend_Loader_Autoloader::getInstance()->registerNamespace('Internal');
        // plugin
        Zend_Loader_Autoloader::getInstance()->registerNamespace('Hook');
        Zend_Loader_Autoloader::getInstance()->registerNamespace('Controller');
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new Hook_Main());
        
        // server paths        
        $serverPaths = new stdClass();
        // protocol
        $serverPaths->protocol  = isset($_SERVER['HTTPS']) ? 'https' : 'http';
        // protocol2
        $serverPaths->https     = isset($_SERVER['HTTPS']) ? : FALSE;
        // server name
        $serverPaths->name      = $_SERVER['SERVER_NAME'];
        // server port
        $serverPaths->port      = $_SERVER['SERVER_PORT'];
        // base dir
        $serverPaths->baseDir   = dirname($_SERVER["SCRIPT_NAME"]);
        // base url
        $serverPaths->baseUrl   =
            "{$serverPaths->protocol}://{$serverPaths->name}:" .
            "{$serverPaths->port}{$serverPaths->baseDir}/";
        ZReg::set('serPaths', $serverPaths);
    }
    
    protected function _initSession()
    {        
        // imitate user session
        ZReg::set('user', new stdClass());
        ZReg::get('user')->lang = ZReg::get('config')->i18n->defaultLanguage;
    }
    
    protected function _initZFDebug()
    {
        Zend_Loader_Autoloader::getInstance()->registerNamespace('ZFDebug');
        
        $this->bootstrap('FrontController');
        $front = $this->getResource('FrontController');
        if ($this->hasOption('zfdebug'))
        {
            $zfdebug = new ZFDebug_Controller_Plugin_Debug($this->getOption('zfdebug'));
            $front->registerPlugin($zfdebug);
        }
    }
    
    protected function _initLibs()
    {   
        // General\Helper
        Zend_Loader::loadFile('Helper.php', LIBRARY_PATH . '/Internal/General');
        // I18n
        $this->_initLibI18n();
        // DbPatcher
        $this->_initLibDbPatcher();
        // SwiftMailer
        Zend_Loader::loadFile('swift_required.php',
            LIBRARY_PATH . '/SwiftMailer/lib');
        $this->_initLibSwiftMailer();
        // phpOauth
        Zend_Loader::loadFile('bootstrap.php',
            LIBRARY_PATH . '/phpOauth/apps');
    }
    
    private function _initLibI18n()
    {
        $i18nConfig = ZReg::get('config')->i18n;
        $config = array(
                'defaultLanguage'   => $i18nConfig->defaultLanguage,
                'paths'             => iterator_to_array($i18nConfig->paths));
        
        // I18n
        Zend_Loader::loadFile('c_i18n_property.php', LIBRARY_PATH . '/I18n');
        $oI18n = new cI18nProperty();
        $oI18n->init($config);
        $oI18n->setLanguage(ZReg::get('user')->lang);
        
        $oI18n->load('main');
        
        ZReg::set('i18n', $oI18n);
        
        // set default locale
        $locale = new \Zend_Locale(\ZReg::get('user')->lang);
        \ZReg::set('Zend_Locale', $locale);
    }
    
    private function _initLibDbPatcher()
    {
        $DbPatcherConfig = array();
        $DbPatcherConfig['db'] =
            iterator_to_array(ZReg::get('config')->database->params);
        $DbPatcherConfig['paths'] =
            iterator_to_array(ZReg::get('config')->dbp->paths);
        $DbPatcherConfig['else'] =
            iterator_to_array(ZReg::get('config')->dbp->else);
        $DbPatcherConfig['else']['timezone'] = date_default_timezone_get();
        
        Zend_Loader::loadFile('c_db_patcher.php', LIBRARY_PATH . '/DbPatcher');
        
        $DbPatcher = new cDbPatcher($DbPatcherConfig);
            $DbPatcher->run();
        unset($DbPatcher);
    }
    
    private function _initLibSwiftMailer()
    {
        $transport  = Swift_SendmailTransport::
                        newInstance(ZReg::get('config')->mailer->sendmail);
        $mailer     = Swift_Mailer::newInstance($transport);
        
        ZReg::set('mailer', $mailer);
    }
    
    protected function _initView()
    {
        Zend_Loader_Autoloader::getInstance()->registerNamespace('Twig');
        
        $view = new Internal_Twig_View(APPLICATION_PATH . '/views/scripts');
        
        // always load the main-index template
        //$view->loadTemplate('index/main_index.html.twig');
        
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $viewRenderer->setViewSuffix('html.twig');
        $viewRenderer->setView($view);
        
        return $view;
    }
    
    protected function _initValidator()
    {
        Zend_Validate_Abstract::setDefaultTranslator(
            new Zend_Translate(
                array(
                    'adapter' => 'array',
                    'content' => APPLICATION_PATH . '/language/resources',
                    'locale'  => ZReg::get('user')->lang,
                    'scan' => Zend_Translate::LOCALE_DIRECTORY
                )
            ));
    }
    
    protected function _initDb()
    {
        $db = Zend_Db::factory(ZReg::get('config')->database);

        ZReg::set('db', $db);
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
    }
    
    protected function _initModels()
    {
        \Helper\loadModel('main', 'users');
        \Helper\loadModel('main', 'usersProfile');
    }
}